package com.yf.utils;

import io.rong.RongCloud;
import io.rong.methods.group.Group;
import io.rong.methods.user.User;
import io.rong.models.Result;
import io.rong.models.group.GroupMember;
import io.rong.models.group.GroupModel;
import io.rong.models.response.GroupUserQueryResult;
import io.rong.models.response.TokenResult;
import io.rong.models.user.UserModel;
import org.junit.Test;

/**
 * User: Administrator
 * Date: 2018-10-22
 * Time: 11:31
 * Description:
 */
public class RongCloudUtils {
    private static final String appKey = "ik1qhw09ipxtp";
    private static final String appSecret = "kBs7MTkdEQOr9";

    /**
     * 获取RongCloud实例
     */
    private static RongCloud getRongCloud(){
        return RongCloud.getInstance(appKey, appSecret);
    }

    /**
     * 注册用户，生成用户在融云的唯一身份标识 Token
     */
    public static TokenResult getRongCloudToken(String userId,String userName,String userAvatar){

        RongCloud rongCloud = getRongCloud();
        User User = rongCloud.user;

        UserModel user = new UserModel()
                .setId(userId)
                .setName(userName)
                .setPortrait(userAvatar);
        TokenResult result = null;
        try {
            result = User.register(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 刷新用户，
     */
    public static Result updateUsers(String userId,String userName,String userAvatar){

        RongCloud rongCloud = getRongCloud();
        User User = rongCloud.user;

        UserModel user = new UserModel()
                .setId(userId)
                .setName(userName)
                .setPortrait(userAvatar);
        Result result = null;
        try {
            result = User.update(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 用户加群
     */
    @Test
    public static Result joinGroup(GroupMember[] members,String groupId){
        RongCloud rongCloud = getRongCloud();
        Group Group = rongCloud.group;

//        GroupMember[] members = {new GroupMember().setId("ghJiu7H1"),new GroupMember().setId("ghJiu7H2")};

        /**
         * API 文档: http://www.rongcloud.cn/docs/server_sdk_api/group/group.html#join
         *
         * 用户加入指定群组
         *
         */
        GroupModel group = new GroupModel()
                .setId(groupId)
                .setMembers(members);
        Result groupJoinResult = null;
        try {
            groupJoinResult = Group.join(group);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("join:  " + groupJoinResult.toString());
        return groupJoinResult;
    }

    /**
     * 获取群信息
     */

    public static GroupUserQueryResult getGroup(String groupId){
        RongCloud rongCloud = getRongCloud();
        Group Group = rongCloud.group;

        GroupModel group  = new GroupModel().setId("groupId");
        GroupUserQueryResult getMemberesult = null;
        try {
            getMemberesult = Group.get(group);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("group getMember:  " + getMemberesult.toString());
        return getMemberesult;
    }
}
